package com.footinteraction;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;

public class SignalProvider extends ContentProvider {

	SignalDBHelper mOpenHelper = null;

	public static final String AUTHORITY = "com.footinteraction";
	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
			+ "/signals");

	public static final int SIGNALS = 1;
	public static final int SIGNALS_ID = 2;

	public static final UriMatcher sUriMatcher;

	static {
		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		sUriMatcher.addURI(AUTHORITY, "signals", SIGNALS);
		sUriMatcher.addURI(AUTHORITY, "signals/#", SIGNALS_ID);
	}

	@Override
	public boolean onCreate() {
		// TODO Auto-generated method stub
		mOpenHelper = new SignalDBHelper(getContext());

		if (mOpenHelper != null) {
			return true;
		}

		return false;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();

		if (sUriMatcher.match(uri) != SIGNALS) {
			throw new IllegalArgumentException("Unknown URI : " + uri);
		}

		long rowId = db.insert(SignalDBHelper.TABLE_NAME, null, values);

		if (rowId > 0) {
			return ContentUris.withAppendedId(CONTENT_URI, rowId);
		}

		throw new SQLException("Failed to insert row into : " + uri);
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		
		int count;
		
		switch (sUriMatcher.match(uri)){
		case SIGNALS:
		{
			count = db.update(SignalDBHelper.TABLE_NAME, values, selection, selectionArgs);
			break;
		}
		case SIGNALS_ID:
		{
			String itemIndex = uri.getPathSegments().get(1);
			
			count = db.update(SignalDBHelper.TABLE_NAME, values, SignalDBHelper.KEY_ID + "=" + itemIndex + (TextUtils.isEmpty(selection)? "" : " AND (" + selection + ')'), selectionArgs);
			break;
		}
		
		default:
		{
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		}
		
		
		return count;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteDatabase db = mOpenHelper.getReadableDatabase();
		Cursor c = null;
		
		// TODO Auto-generated method stub
		switch (sUriMatcher.match(uri)){
		case SIGNALS:
		{
			c = db.query(SignalDBHelper.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
			break;
		}
		case SIGNALS_ID:
		{
			String itemIndex = uri.getPathSegments().get(1);
			
			c = db.query(SignalDBHelper.TABLE_NAME, projection, SignalDBHelper.KEY_ID + "=" + itemIndex + (TextUtils.isEmpty(selection) ? "" : " AND (" + selection + ')'), selectionArgs, null, null, sortOrder);
			break;
		}
		default:
		{
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		}
		
		return c;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		
		int count;
		
		switch (sUriMatcher.match(uri)){
		case SIGNALS:
		{
			count = db.delete(SignalDBHelper.TABLE_NAME, selection, selectionArgs);
			break;
		}
		case SIGNALS_ID:
		{
			String itemIndex = uri.getPathSegments().get(1);
			
			count = db.delete(SignalDBHelper.TABLE_NAME, SignalDBHelper.KEY_ID + "=" + itemIndex + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs);
			break;
		}
		
		default:
		{
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		
		}
		
		return count;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		switch (sUriMatcher.match(uri)){
		case SIGNALS:
		{
			return "vnd.signals.cursor.dir/signals";
		}
		case SIGNALS_ID:
		{
			return "vnd.signals.cursor.item/signals";
		}
		default:
		{
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		}
	}
}









